//
//  TJLive_MainViewExtension.swift
//  KangBoss
//
//  Created by 庞成龙 on 2022/10/13.
//

import Foundation

extension UIView {
    /// 计算点击位置透明图
    /// - Parameter point: 点击位置坐标
    /// - Returns: 透明值
    func alphaOfPointFromLayer(point: CGPoint) -> CGFloat {
        var pixel = [UInt8](repeatElement(0, count: 4))
//        var pixel: [UInt8] = [0, 0, 0, 0]
        let colorSpace = CGColorSpaceCreateDeviceRGB()
        let context = CGContext(data: &pixel, width: 1, height: 1, bitsPerComponent: 8, bytesPerRow: 4, space: colorSpace, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)
        context?.setBlendMode(.copy)
        context?.translateBy(x: -point.x, y: -point.y)
        if let context = context {
            layer.render(in: context)
        }
        let alpha = CGFloat(pixel[3]) / CGFloat(255.0)
        
        return alpha
    }
}
